package com.ctcd.security.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.excel.EasyExcel;
import com.ctcd.security.listener.DemoDataListener;
import com.ctcd.security.pojo.User;
import com.ctcd.security.pojo.UserData;

import java.util.*;

/**
 * @version 1.0
 * @Author tw
 * @Date 2024/12/17 9:12
 * @注释
 */
public class ExcelOpration {

    static  String filePath = "C:\\Users\\lenovo\\Desktop\\a.xlsx";


    //获取每一行的数据
    public static List<UserData> getAll(){
        DemoDataListener<UserData> demoDataListener = new DemoDataListener<>();
        EasyExcel.read(filePath, UserData.class, demoDataListener).sheet().doRead();
        List<UserData> datas = demoDataListener.getDatas();
        datas.remove(0);
       return datas;
    }

    //打乱中队列数据
    public  static List<Object> shift(){
        List<Object> excel = getExcel(filePath);
        Collections.shuffle(excel);
        return excel;
    }

    //获取中队数据
    public static  List<Object> getExcel(String filePath){
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(filePath));
        return reader.readColumn(2, 3);
    }

    //插入到表数据中
    public static  List<User> updateDept(List<UserData> userData, List<Object> shift){

        for (int i = 0; i < shift.size(); i++) {
            userData.get(i).setDept((String) shift.get(i));
        }

        //插入之后转换为user类型
         List<User> users =new ArrayList<>();
        userData.forEach(v->{
              User user = new User(Integer.valueOf(v.getId()),v.getName(),v.getDept());
              users.add(user);
          });
        return users;
    }

    //计数
    public static Map<String,Integer> count(){
        List<Object> shift = shift();
        // 使用HashMap来存储和统计每个中队的数量
        Map<String, Integer> squadronCounts = new HashMap<>();
        // 遍历列表，统计每个中队的数量
        for (Object squadron : shift) {
            squadronCounts.put((String) squadron, squadronCounts.getOrDefault(squadron, 0) + 1);
        }

        return squadronCounts;
    }

}
